package pl.edu.agh.mobicentsVoIP.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.sip.SipFactory;

import org.apache.log4j.Logger;


public class ActiveCallsHttpServlet extends HttpServlet{

	private static Logger logger = Logger.getLogger(ActiveCallsHttpServlet.class);
    private SipFactory sipFactory;  
	
	@Override
    public void init(ServletConfig config) throws ServletException {                
            super.init(config);
            logger.info("the ActiveCallsHttpServlet has been started");
            try {                   
                    // Getting the Sip factory from the JNDI Context
                    Properties jndiProps = new Properties();                        
                    Context initCtx = new InitialContext(jndiProps);
                    Context envCtx = (Context) initCtx.lookup("java:comp/env");
                    sipFactory = (SipFactory) envCtx.lookup("sip/pl.edu.agh.mobicentsVoIP.servlets.Application/SipFactory");
                    logger.info("Sip Factory ref from JNDI : " + sipFactory);
            } catch (NamingException e) {
                    throw new ServletException("Uh oh -- JNDI problem !", e);
            }
    }

	public void doGet (HttpServletRequest request,HttpServletResponse response) 
			throws ServletException, IOException{
		
		// Write the output html
        PrintWriter     out;
        response.setContentType("text/html");
        out = response.getWriter();
        
        // Just redirect to the index
        out.println("<HTML><META HTTP-EQUIV=\"Refresh\"CONTENT=\"0; URL=index.jsp\"><HEAD><TITLE></HTML>");
        out.close();
        
	}
}
